//
//  HomeViewController.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2020/5/26.
//  Copyright © 2020 杨东宝. All rights reserved.
//

import UIKit
import EmptyDataSet_Swift
class HomeViewController: YDBBaseViewController {
    
    var homeViewModel = HomeViewModel()
    
    //MARK:-消息按钮
    lazy var messageBtn:UIButton = {
        let messageBtn = UIButton.init(type: .custom)
        messageBtn.setImage(imageNamed("messageHome"), for: .normal)
        messageBtn.size = CGSize(width: DIMENSION_VALUE(40), height: DIMENSION_VALUE(34))
        messageBtn.addTarget(self, action: #selector(messageBtnClick(_ : )), for: .touchUpInside)
        return messageBtn
    }()
    //MARK:消息数量
    lazy var label5: UILabel = {
        let label = UILabel()
        label.text = "6"
        label.font = DIMENSION_FONT_OF_SIZE(12)
        label.backgroundColor = HexRGB(0xE02020)
        label.textColor = HexRGB(0xffffff)
        label.layer.cornerRadius = DIMENSION_VALUE(7)
        label.layer.masksToBounds = true
        label.textAlignment = .center
        label.frame = CGRect(x:  DIMENSION_VALUE(20), y: DIMENSION_VALUE(3), width: DIMENSION_VALUE(14), height: DIMENSION_VALUE(14))
        messageBtn.addSubview(label)
        return label
    }()
    //MARK:-搜索框
    lazy var searchTextField:UITextField = {
        let searchTextField = UITextField.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth - 50, height: 30))
        searchTextField.placeholder = "查英文或中文"
        searchTextField.font = DIMENSION_FONT_OF_SIZE(12)
        searchTextField.textColor = hexColor(0x33333,0xA7A7B3)
        searchTextField.backgroundColor = hexColor(0xE7E7E7,0x20202C)
        searchTextField.returnKeyType = .search;
        searchTextField.delegate = self
        let rightView = UIView.init(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        
        let  rightbtn = UIButton.init(type: .custom)
        rightbtn.frame = CGRect(x:0,y:0,width: 30, height:  30);
        rightbtn.contentMode = .center;
        rightbtn.setImage(imageNamed("searchIcon"), for: .normal)
        rightView.addSubview(rightbtn)
        searchTextField.leftView = rightView;
        searchTextField.leftViewMode = .always;
        
        return searchTextField
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.titleView = searchTextField
        self.navigationItem.title = "首页"
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: messageBtn)
        
        self.view.addSubview(tableView)
        tableView.separatorStyle = .none;
        tableView.emptyDataSetSource = self
        tableView.emptyDataSetDelegate = self
        tableView.myh_footer?.isHidden = true
        self.tableView.register(HomeViewCell.self, forCellReuseIdentifier: "HomeViewCell")
        tableView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(5)
        }
        //登录监听
        NotificationCenter.default.addObserver(self, selector:  #selector(setupLoadData), name:loginSuc, object: nil)
        //刷新页面监听
        NotificationCenter.default.addObserver(self, selector:  #selector(setupLoadData), name:nRootVCRefresh, object: nil)
        //未读消息数量
        NotificationCenter.default.addObserver(self, selector: #selector(unreadMessageCount(_ :)), name:  NSNotification.Name(rawValue: KNotificationUnreadMsg), object: nil)
        self.setupLoadData()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    @objc override func setupLoadData() {
        homeViewModel.getHomeViewDetail { [weak self](result) in
            self?.tableView.myh_header?.endRefreshing()
            self?.tableView.reloadData()
        }
    }
    //MARK:-首页消息点击
    @objc func messageBtnClick(_ btn:UIButton) {
        if !userManger.isLogined {
            return
        }

    }
    
    //MARK:-消息的数量通知
    @objc func unreadMessageCount(_ result:Notification) {
        
        guard let dict = result.userInfo else { return }
        
        guard let unreadCount = dict["unreadCount"] else { return }
        
        if (unreadCount as! String  == "0") {
            label5.isHidden = true
        }else{
            label5.isHidden = false
            label5.text = unreadCount as? String
        }
    }
    
}
// MARK:-TableView代理
extension HomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.homeViewModel.homeModel != nil && (self.homeViewModel.homeModel?.book_word_num  ?? 0 > 0)) {
            return 1
        }
        return 0
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : HomeViewCell = tableView.dequeueReusableCell(withIdentifier: "HomeViewCell")! as! HomeViewCell
        cell.selectionStyle = .none
        cell.model = self.homeViewModel.homeModel!
        return cell
    }
}
extension HomeViewController:UITextFieldDelegate{
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
       
        return false
    }
}
extension HomeViewController:EmptyDataSetSource,EmptyDataSetDelegate{
    
    func customView(forEmptyDataSet scrollView: UIScrollView) -> UIView? {
    
        let customView = UIView();
        return customView
    }
}
